######################################################################################
#
# This file was provided as part of the SharePoint Mystery Deployment package
# Created by Christopher Kimbell (http://sharepointmystery.com)
#
######################################################################################

# -----------------------------------------------------------------
# Creates an object used to manipulate the Secure Store Service
# -----------------------------------------------------------------
function New-SecureStoreService()
{
    $obj=new-object PSObject -Property @{
      Name = "Secure Store Service";
      Proxy = "Secure Store Service Proxy";
      Pool = New-ApplicationPool;
      DB = New-Database;
      Service = New-SupportingService;
      AuditingEnabled = $true;
      AuditlogMaxSize = "60";
      Sharing = $false;
      Passphrase = "";
    }
    $obj.Service.Name = "Secure Store Service"
    # -----------------------------------------------------------------
    # Validates the information in the object
    # -----------------------------------------------------------------
    Add-Method $obj "Validate" `
    {
        Write-Operation "Validating Secure Store Service"
        $this.Pool.Validate()
        $this.Service.Validate()
        $this.DB.Validate()
        Validate-NotEmpty "Name" $this.Name
        Validate-NotEmpty "Proxy" $this.Proxy 
        Validate-NotEmpty "Passphrase" $this.Passphrase 
    }
    # -----------------------------------------------------------------
    # Provisions the Service
    # -----------------------------------------------------------------
    Add-Method $obj "Provision" `
    {
        $sa = Get-SPServiceApplication  | Where-Object {$_.DisplayName -eq $this.Name}

        if($sa -eq $null)
        {
            Write-Operation "Creating $($this.Name)"
            $pool = $this.Pool.Provision()
            $this.DB.Provision()
            $this.DB.AddRole($farm.DeploymentAccount, "db_owner")
            $this.DB.AddRole($this.Pool.Account, "db_owner")
            
            $sa = New-SPSecureStoreServiceApplication -Name $this.Name `
                    -ApplicationPool $pool `
                    -AuditingEnabled:$this.AuditingEnabled `
                    -AuditlogMaxSize $this.AuditlogMaxSize `
                    -DatabaseName $this.DB.FUllName `
                    -DatabaseServer $this.DB.Server `
                    -Sharing:$this.Sharing
        }
        else
        {
            Write-OperationDone "$($this.Name) has already been provisioned"  
        } 
         
        $sa = Get-SPServiceApplication  | Where-Object {$_.DisplayName -eq $this.Name}         
        $proxy = Get-SPServiceApplicationProxy  | Where-Object {$_.DisplayName -eq $this.Proxy}  
        if($proxy -eq $null)
        {
            $proxy = New-SPSecureStoreServiceApplicationProxy -Name $this.Proxy -ServiceApplication $sa
        }
        else
        {
            Write-OperationDone "$($this.Proxy) has already been provisioned"  
        }
        
        Update-SPSecureStoreMasterKey -ServiceApplicationProxy $proxy -Passphrase $this.Passphrase
        
        $this.Service.Provision()
        
        return $sa
    }
    # -----------------------------------------------------------------
    # Unprovisions the Service
    # -----------------------------------------------------------------
    Add-Method $obj "Unprovision" `
    {
        Remove-ServiceApplicationProxy $this.Proxy
        Remove-ServiceApplication $this.Name
        $this.Service.Unprovision()
    }
    return $obj
}
